﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_ManageCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataGrid(0);

        }
        else
        {

        }
    }
    #region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
        try
        {
            grdCategory.DataSource = Category_Session.Get_CategoryAsTable("Cate_ParentID = 0", "");
            grdCategory.DataBind();
        }
        catch (Exception ex)
        {
           // throw ex;
        }

    }
    #endregion
    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        //WebMsgBox.Show("siem ann");
        /*
        try
        {
            string s = ProductSelectedArray();
            if (string.IsNullOrEmpty(s)) return;

            //xoa anh
            DataTable lst = Product_BAL.Get_ProductAsTable("Pro_ID In (" + s + ")", "");
            if (lst == null || lst.Rows.Count == 0) return;

            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Product/";
            foreach (DataRow r in lst.Rows)
            {
                ExtendFunctional.DeleteImageInFolder(_sFolder + "/" + r["Pro_Image"].ToString());
            }

            Product_BAL.Delete_Product_Where("Pro_ID In (" + s + ")");
            MessageBox.Show("Đã xóa thành công.");
            BindDataGrid(1);
        }
        catch (Exception ex)
        {
            throw ex;
        }*/
    }

    protected void grdCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCategory.PageIndex = e.NewPageIndex;
        BindDataGrid(grdCategory.PageIndex);
    }
    protected void grdCategory_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        string strCategoryID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;
        switch (strCommand)
        {
            case "delete_Category":
                try
                {
                    Category category = Category_Session.Get_Category_ByID(int.Parse(strCategoryID));
                    if (category == null) return;
                    Category_Session.Delete_Category(category);// muon xoa dc thi phai xoa het khoa ngoai cai da met ge
                    WebMsgBox.Show("Đã xóa thành công.");
                    BindDataGrid(0);
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
                break;
            case "active_Category":
                try
                {
                    Category category = Category_Session.Get_Category_ByID(int.Parse(strCategoryID));
                    if (category == null) return;
                    category.Cate_Status = !category.Cate_Status;
                    Category_Session.Update_Category(category);// chua update dc nek
                    WebMsgBox.Show("Đã kích hoạt thành công.");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;   
        }
    }
    //Lay ID tren cac cot duoc checked	
    private string CategorySelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdCategory.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }

    protected void grdCategory_RowDataBound(object sender, GridViewRowEventArgs e)// chay dung roi , nhung phai update dc thi moi dc
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "Cate_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "~/admin/images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "~/admin/images/uncheck.gif";
                    }
                }
            }
        }
    }
}
